<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Paicha;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class PaichaController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Paicha(), function (Grid $grid) {

            $grid->model()->orderBy('jianchadate', 'desc');

            $grid->column('jianchadate');
            $grid->column('leixing');
            $grid->column('jiancharenyuan');
            $grid->column('yinhanwenti');

            $grid->column('yinhuanpics')->display(function ($imgs) {
                $pics = json_decode($imgs);
                if ($pics) {
                    return $pics[0];
                }
            })->image();

            $grid->column('zhenggaipics')->display(function ($imgs) {
                $pics = json_decode($imgs);
                if ($pics) {
                    return $pics[0];
                }
            })->image();

            $grid->column('wanchengdate');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Paicha(), function (Show $show) {
            $show->field('id');
            $show->field('jianchadate');
            $show->field('leixing');
            $show->field('jiancharenyuan');
            $show->field('yinhanwenti');
            $show->yinhuanpics()->file();
            $show->zhenggaipics()->image();
            $show->field('wanchengdate');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Paicha(), function (Form $form) {
            $form->display('id');
            $form->date('jianchadate')->required();;
            $form->radio('leixing')->options(['上级检查' => '上级检查', '消防安全检查日' => '消防安全检查日', '节前检查' => '节前检查', '其他' => '其他'])->default("上级检查")->required();
            $form->text('jiancharenyuan');
            $form->textarea('yinhanwenti');

            $form->multipleFile('yinhuanpics')->accept('jpg,png,gif,jpeg,mimes:doc,docx,xlsx')->limit(9)->autoUpload()->maxSize(20480)->saving(function ($files) {
                return json_encode($files);
            });

            $form->multipleImage('zhenggaipics')->accept('jpg,png,gif,jpeg')->limit(9)->autoUpload()->maxSize(20480)->saving(function ($files) {
                return json_encode($files);
            });
            $form->date('wanchengdate');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
